﻿using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using ApplicationLibrary.Preferences;
using ApplicationLibrary.SqlConnector;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace ApplicationLibrary.DBManager
{
    public partial class printerForm : WinFormsUI.Docking.DockContent
    {
        public enum PrintMode { Preview, Print };

        private Dictionary<string, ParameterValues> ParameterList = null;
        private PreferencesManager<PrinterPref> myPrinter = null;
        private ReportClass myReport = null;
        private PrinterPref pPref = null;
        private PrintMode printMode;
        private List<string> clickableColumn = new List<string>();

        public delegate void EmailArgsEventHanlder(object sender, EmailArgs e);

        public event EmailArgsEventHanlder SendEmail;

        public delegate void LoadAddressEventHandler(ApplicationLibrary.Emailer.emailAddress ea);

        public event LoadAddressEventHandler LoadAddress;

        protected virtual void OnDoubleClickColumn(string column, string value)
        {
        }

        public printerForm(string application, PrintMode printMode, PrintType printType)
        {
            InitializeComponent();
            this.printMode = printMode;
            myPrinter = new PrinterPreferencesManager(application);

            pPref = myPrinter.ReadPreference();

            foreach (String printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
                tscPrinter.Items.Add(printer.ToString());

            switch (printType)
            {
                case PrintType.Letter:
                    tscPrinter.Text = pPref.Letter.PrinterName;
                    tsnCopies.Text = pPref.Letter.NumPage.ToString();
                    tsbCollate.Checked = pPref.Letter.Collate;
                    break;

                case PrintType.Envelope:
                    tscPrinter.Text = pPref.Envelope.PrinterName;
                    tsnCopies.Text = pPref.Envelope.NumPage.ToString();
                    tsbCollate.Checked = pPref.Envelope.Collate;
                    break;
            }
        }

        public void ShowReport(ReportClass Report, object DataSource)
        {
            myReport = Report;
            ShowReport(Report, DataSource, "", null);
        }

        public string ExportToPdf(string filename)
        {
            filename = filename.Replace(" ", string.Empty).Replace("/", "_");
            string file = System.IO.Path.ChangeExtension(System.IO.Path.Combine(System.IO.Path.GetTempPath(), filename), "pdf");
            myReport.ExportToDisk(ExportFormatType.PortableDocFormat, file);
            return file;
        }

        public void ShowReport(ReportClass Report, object DataSource, string DetailName, object Detail)
        {
            Report.SetDataSource(DataSource);
            if (Detail != null)
                Report.Subreports[DetailName].SetDataSource(Detail);
            if (ParameterList != null)
            {
                foreach (KeyValuePair<string, ParameterValues> kvp in ParameterList)
                    Report.ParameterFields[kvp.Key].CurrentValues = kvp.Value;
            }
            if (printMode == PrintMode.Preview)
                crystalReportViewer1.ReportSource = Report;
            else
                tsbPrint_Click(this, EventArgs.Empty);
        }

        public void SetParameterValue(string paramName, object paramValue)
        {
            if (ParameterList == null)
                ParameterList = new Dictionary<string, ParameterValues>();

            if (ParameterList.ContainsKey(paramName))
                ParameterList.Remove(paramName);

            ParameterDiscreteValue parameterDiscreteValue = new ParameterDiscreteValue();
            parameterDiscreteValue.Value = paramValue;
            ParameterValues currentParameterValues = new ParameterValues();
            currentParameterValues.Add(parameterDiscreteValue);

            ParameterList.Add(paramName, currentParameterValues);
        }

        public void ChangeParameterValue(string paramName, object paramValue)
        {
            if (ParameterList != null && ParameterList.ContainsKey(paramName))
            {
                ParameterDiscreteValue parameterDiscreteValue = new ParameterDiscreteValue();
                parameterDiscreteValue.Value = paramValue;
                ParameterValues currentParameterValues = new ParameterValues();
                currentParameterValues.Add(parameterDiscreteValue);
                myReport.ParameterFields[paramName].CurrentValues = currentParameterValues;
            }
        }

        private void tsbPrint_Click(object sender, EventArgs e)
        {
            myReport.PrintOptions.PrinterName = tscPrinter.Text;
            myReport.PrintToPrinter(tsnCopies.Value, tsbCollate.Checked, 0, 0);
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tscPrinter_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tscPrinter.Items.Count == 0 || tscPrinter.Text == "")
                return;

            PrinterSettings ps = new PrinterSettings();
            ps.PrinterName = tscPrinter.Text;
        }

        private void tsbEmail_Click(object sender, EventArgs e)
        {
            Emailer.emailForm ef = new Emailer.emailForm();
            ef.LoadAddress += new Emailer.emailForm.LoadAddressEventHandler(ef_LoadAddress);
            if (SendEmail != null)
            {
                EmailArgs emailArgs = new EmailArgs();
                SendEmail(this, emailArgs);
                ef.Address = emailArgs.Address;
                ef.Subject = emailArgs.Subject;
                ef.Body = emailArgs.Body;
                ef.Attachment = emailArgs.Attachment;
            }
            ef.ShowDialog();
            ef.Close();
        }

        private void ef_LoadAddress(Emailer.emailAddress ea)
        {
            if (LoadAddress != null)
                LoadAddress(ea);
        }

        public void AddClickableColumn(iColumn column)
        {
            if (!clickableColumn.Contains(column.Name))
                clickableColumn.Add(column.Name);
        }

        private void crystalReportViewer1_DoubleClickPage(object sender, CrystalDecisions.Windows.Forms.PageMouseEventArgs e)
        {
            if (clickableColumn.Contains(e.ObjectInfo.Name))
                OnDoubleClickColumn(e.ObjectInfo.Name, e.ObjectInfo.Text);
        }

        private void tsbCollate_CheckedChanged(object sender, EventArgs e)
        {
            tsbCollate.Text = tsbCollate.Checked
                                ? "1,2 1,2"
                                : "1,1 2,2";
        }
    }

    public class EmailArgs : EventArgs
    {
        public object Sender { get; set; }

        public string Address { get; set; }

        public string Subject { get; set; }

        public string Body { get; set; }

        public string Attachment { get; set; }
    }
}